$(function () {
    obj.init();
});

var obj = new function () {
    return {
        outDataGrid: null,
        inDataGrid: null,
        params: {
            schoolId: null,
            groupId: null
        },
        dom: {
            confirm: null
        },
        init: function () {
            this.params.groupId = xhTool.getQuery('groupId');
            this.params.schoolId = xhTool.getQuery('schoolId');

            this.dom.confirm = window.top.xhTop.getModalConfirmDom();

            this.inDataGrid = new xhDataGrid($('#list_in'), 'Appgroup/getAppGroupUser', this.params);
            this.outDataGrid = new xhDataGrid($('#list_out'), 'Appgroup/getUserForAppGroup', this.params);
            this.inDataGrid.init().activeBtn($('#bt_out'), null, 'bs');
            this.outDataGrid.parseToolbar().init().activeBtn($('#bt_in'), null, 'bs');
            xhExtra.clearLoadSuccess(this.outDataGrid);

            this.listener();
        },
        listener: function () {
            var $this = this;
            $('#bt_in').click(function () {
                $this.funObj().moveUserIn();
            });
            $('#bt_out').click(function () {
                $this.funObj().moveUserOut();
            });
            $this.dom.confirm.click(function () {
                $this.funObj().submitUser();
            });
            xhTool.bindDocKeyDown($('#bt_list_out_search').click(function () {
                $this.funObj().searchUser();
            }));
        },
        funObj: function () {
            var $this = this;
            return {
                searchUser: function () {
                    $this.outDataGrid.reload({
                        history: xhExtra.getValue($('#list_out_history')),
                        userType: xhExtra.getValue($('#list_out_user_type')),
                        subject: xhExtra.getValue($('#list_out_subject')),
                        items: xhExtra.getValue($('#list_out_item')),
                        vague: xhExtra.getValue($('#list_out_vague')),
                    })
                },
                moveUserIn: function () {
                    var rows = $this.outDataGrid.command('getSelections');
                    $.each(rows, function(index, row){
                        if($this.inDataGrid.command('getRowIndex', row.iUserId) >= 0){
                            return true;
                        }
                        $this.inDataGrid.command('appendRow', row);
                    });
                    $this.outDataGrid.clear();
                },
                moveUserOut: function () {
                    var rows = $this.inDataGrid.command('getSelections');
                    for(var i = rows.length - 1; i >= 0; i--){
                        $this.inDataGrid.command('deleteRow', $this.inDataGrid.command('getRowIndex', rows[i].iUserId));
                    }
                    $this.inDataGrid.clear();
                },
                submitUser: function () {
                    var rows    = $this.inDataGrid.command('getRows'),
                        userIds = [];
                    $.each(rows, function(index, row){
                        userIds.push(row.iUserId);
                    });
                    if (userIds.length <= 0) {
                        window.top.swal({
                            title: '用户列表为空！',
                            text: '是否确认提交？',
                            showCancelButton: true,
                            cancelButtonText: '取消',
                            confirmButtonText: '提交',
                            allowOutsideClick: true,
                            showCloseButton: true,
                            type: 'warning'
                        }).then(function(isConfirm) {
                            if (!isConfirm) {
                                return false;
                            }
                            $this.funObj().modifyGroupUser(userIds);
                        }, function(dismiss) {
                            xhExtra.setBtnNormal($this.dom.confirm);
                        });
                    }
                    else {
                        $this.funObj().modifyGroupUser(userIds);
                    }
                },
                modifyGroupUser: function (userIds) {
                    xhExtra.ajaxRequest(
                        'appgroup/modifyAppGroupUser',
                        {groupId: $this.params.groupId, userIds: userIds},
                        {check: false},
                        function (result) {
                            if(xhExtra.checkError(result)) {
                                return false;
                            }
                            window.top.xhTop.showMessage({
                                type: 'success',
                                title: result.msg
                            });
                            window.top.xhTop.getActiveFrame().obj.dataGrid.reload();
                            window.top.xhTop.closeModal();
                        }
                    );
                }
            }
        }
    }
};

/**
 * create by ck on 2017/7/2.
 */
// $(document).ready(function() {
//     var groupId     = xhTool.getQuery('groupId'),
//         schoolId    = xhTool.getQuery('schoolId'),
//         $dataGridIn = new xhDataGrid($('#list_in'), 'Appgroup/getAppGroupUser', {groupId: groupId}),
//         $dataGridOut= new xhDataGrid($('#list_out'), 'User/getDgData', {iSchoolId: schoolId});
//     $dataGridIn.init().activeButton(['bt_out'], [], 'bs');
//     $dataGridOut.parseToolbar(bindOutListSearch).init().activeButton(['bt_in'], [], 'bs');
//     xhExtra.clearLoadSuccess($dataGridOut);
//     //点击添加按钮，复制信息到关联表中，去除重复记录
//     $('#bt_in').click(function(){
//         var rows = $dataGridOut.list.datagrid('getSelections');
//         $.each(rows, function(index, row){
//             if($dataGridIn.list.datagrid('getRowIndex', row.iUserId) >= 0){
//                 return true;
//             }
//             $dataGridIn.list.datagrid('appendRow', row);
//         });
//         $dataGridOut.clear();
//     });
//     //点击移除按钮，从关联变中去除记录
//     $('#bt_out').click(function(){
//         var rows = $dataGridIn.list.datagrid('getSelections');
//         for(var i = rows.length - 1; i >= 0; i--){
//             $dataGridIn.list.datagrid('deleteRow', $dataGridIn.list.datagrid('getRowIndex', rows[i].iUserId));
//         }
//         //$.each(rows, function(index, row){
//         //    $dataGridIn.list.datagrid('deleteRow', $dataGridIn.list.datagrid('getRowIndex', row.iUserId));
//         //});
//         $dataGridIn.clear();
//     });
//     //点击顶层保存，获取id并传至后台
//     xhExtra.bindModalConfirm(function(){
//         var rows    = $dataGridIn.list.datagrid('getRows'),
//             userIds = [],
//             $this   = $(this);
//         $this.append(' <i class="fa fa-spinner fa-spin"></i>').attr('disabled', true);
//         $.each(rows, function(index, row){
//             userIds.push(row.iUserId);
//         });
//         if(userIds.length <= 0){
//             window.top.swal({
//                 title: '用户列表为空！',
//                 text: '是否确认提交？',
//                 showCancelButton: true,
//                 cancelButtonText: '取消',
//                 confirmButtonText: '提交',
//                 allowOutsideClick: true,
//                 showCloseButton: true,
//                 type: 'warning'
//             }).then(function(isConfirm) {
//                 if(!isConfirm) return false;
//                 confirmSubmitUser(userIds, $this);
//             }, function(dismiss) {
//                 $this.text($this.text()).attr('disabled', false);
//             });
//             return false;
//         }
//         confirmSubmitUser(userIds, $this);
//     });
//     //提交用户列表
//     function confirmSubmitUser(userIds, $this)
//     {
//         $.ajax({
//             url: baseUrl + 'Appgroup/modifyAppGroupUser',
//             data: {groupId: groupId, userIds: userIds},
//             type: 'POST',
//             dataType: 'json',
//             complete: function(XMLHttpRequest)
//             {
//                 $this.text($this.text()).attr('disabled', false);
//                 var result = XMLHttpRequest.responseJSON || XMLHttpRequest.responseText;
//                 if(xhExtra.checkError(result)) {
//                     return false;
//                 }
//                 window.top.xhTop.showMessage({
//                     type: 'success',
//                     title: result.msg
//                 });
//                 window.top.xhTop.getActiveFrame().obj.dataGrid.reload();
//                 window.top.xhTop.closeModal();
//             }
//         });
//     }
//     //绑定搜索
//     function bindOutListSearch()
//     {
//         var btListOutSearch = $('#bt_list_out_search');
//         btListOutSearch.click(function(){
//             var params = {
//                 iUserType: xhExtra.getValue($('#list_out_user_type')),
//                 search: {
//                     items: xhExtra.getValue($('#list_out_item')),
//                     vague: xhExtra.getValue($('#list_out_vague'))
//                 }
//             };
//             $dataGridOut.reload(params);
//         });
//         xhTool.bindDocKeyDown(btListOutSearch);
//     }
// });